package com.bootdo.jiashan.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.BeanCopierUtil;
import com.bootdo.jiashan.domain.PdtNormDO;
import com.bootdo.jiashan.model.NormOptionModel;
import com.bootdo.jiashan.service.PdtNormService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.jiashan.domain.NormOptionDO;
import com.bootdo.jiashan.service.NormOptionService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 品规属性和品规属性选项值关联表
 * 
 * @author hejinen
 * @email 1992lcg@163.com
 * @date 2019-04-04 22:34:42
 */
 
@Controller
@RequestMapping("/jiashan/normOption")
public class NormOptionController {
	@Autowired
	private NormOptionService normOptionService;
	@Autowired
	private PdtNormService pdtNormService;
	
	@GetMapping()
	@RequiresPermissions("jiashan:product:edit")
	String NormOption(){
	    return "jiashan/normOption/normOption";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jiashan:product:edit")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<NormOptionDO> normOptionList = normOptionService.list(query);
		int total = normOptionService.count(query);
		PageUtils pageUtils = new PageUtils(normOptionList, total);
		return pageUtils;
	}
	
	@GetMapping("/add/{normId}")
	@RequiresPermissions("jiashan:product:edit")
	String add(@PathVariable("normId")Integer normId,String normName,Model model){
		model.addAttribute("normId", normId);
		model.addAttribute("normName", normName);
		return "jiashan/normOption/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jiashan:product:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		NormOptionDO normOption = normOptionService.get(id);
		PdtNormDO pdtNormDO = pdtNormService.get(normOption.getNormId());
		NormOptionModel normOptionModel = new NormOptionModel();
		BeanCopierUtil.copyProperties(normOption,normOptionModel);
		normOptionModel.setNormName(pdtNormDO.getName());
		model.addAttribute("normOption", normOptionModel);
	    return "jiashan/normOption/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jiashan:product:edit")
	public R save( NormOptionDO normOption){
		if(normOptionService.save(normOption)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jiashan:product:edit")
	public R update( NormOptionDO normOption){
		normOptionService.update(normOption);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jiashan:product:edit")
	public R remove( Integer id){
		if(normOptionService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jiashan:normOption:edit")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		normOptionService.batchRemove(ids);
		return R.ok();
	}
	
}
